#region

using Microsoft.Extensions.Options;
using Vin.Extension.Cache;
using Vin.Extension.Framework.Sms.Entities;
using Vin.Extension.Framework.Sms.Model;
using Vin.Extension.Framework.Sms.Repository;
using Vin.Extension.Framework.Sms.ServiceInterface;
using Vin.Tool.AspNetCore.Service;
using Vin.Tool.AspNetCore.Singleton;
using Vin.Tool.Attributes;
using Vin.Tool.Domain.ExceptionModel;

#endregion

namespace Vin.Extension.Framework.Sms.Service;

public class VinSmsService : VinBaseService, IVinSmsService
{
    [Inject] protected readonly SysSmsLogRepository _sysSmsLogRepository = null!;

    // [Inject] protected readonly IOptionsMonitor<VinSmsConfig> _smsConfig = null!;

    protected VinSmsConfig SmsConfig => VinApp.GetConfig<VinSmsConfig>();

    private string MaxCacheKey(string key) => SmsConfig.Code.MaxCacheKey + key;

    public virtual async Task<string> SendCodeAsync(string businessDesc, string receiver, VinSmsSendOption? option = null)
    {
        var count = await CacheFactory.IncrAsync(MaxCacheKey(receiver), TimeSpan.FromMinutes(SmsConfig.Code.MaxExpire));
        if (count > SmsConfig.Code.MaxCount)
        {
            throw new BusinessException("获取过于频繁，请稍后再试");
        }

        return string.Empty;
    }

    public virtual Task<string> SendCodeAsync(string businessDesc, string receiver, string title, string content, object data,
        VinSmsSendOption? option = null)
    {
        throw new NotImplementedException();
    }

    public virtual Task<bool> VerifyCodeAsync(string uuid, string code)
    {
        return Task.FromResult(false);
    }

    public virtual Task SendAsync(string businessDesc, string receiver, string title, string content, object data,
        VinSmsSendOption? option = null)
    {
        throw new NotImplementedException();
    }

    public virtual Task SendAsync(string businessDesc, List<string> receivers, string title, string content,
        object data, VinSmsSendOption? option = null)
    {
        throw new NotImplementedException();
    }

    public virtual async Task<string> GetCodeAsync(string uuid)
    {
        throw new NotImplementedException();
    }

    public async Task<SysSmsLog?> SaveLogAsync(string businessId, string businessDesc, string sender, string channel,
        string receiver, string title,
        string content, bool isSuccess = true, string? result = null, string? template = null,
        object? templateData = null, object? option = null, object? senderInfo = null,
        object? receiverInfo = null)
    {
        return await _sysSmsLogRepository.SaveOrUpdateAsync(new SysSmsLog()
        {
            BusinessId = businessId,
            BusinessDesc = businessDesc,
            Sender = sender,
            SenderInfo = senderInfo ?? new { },
            Channel = channel,
            Receiver = receiver,
            ReceiverInfo = receiverInfo ?? new { },
            Title = title,
            Template = template,
            TemplateData = templateData ?? new { },
            Content = content,
            Option = option ?? new { },
            IsSuccess = isSuccess,
            Result = result,
        });
    }
}